/**
 * 预支台账明细
 */
/**
 * 表单自定义控制器
 */
app.controller('btnCtrl', ['$scope', '$http', BtnController]);
function BtnController($scope, $http) {
	$scope.bindBtn = function(event) {
		switch(event){
			case 'event-to-add': // 新增
			{

				var id = $scope.valueMap["id"];
				var code = $scope.valueMap["code"];
				var name = $scope.valueMap["name"];
				if(code==undefined || code==null || code==''){
					layer.msg("请先选择预支账户编码!");
					return;
				}
				$scope.subGrid.sonlist.push({
					id: null,
					pid:id,
					pCode: code,
					pName: name,
					createTime: '',
					companyName:'',
					companyInfoId : '',
					advanceTypeCode : '',
					advanceTypeName : '',
					vjylx : '',
					detailName : '',
					unitPrice : 0,
					quantity : 0,
					proxyPrice : 0,
					totalPrice : 0,
					proxyTotalPrice : 0,
					vgsbm : '',
					vgsjc : '',
					remark : '',
					annexFile : '0个附件',
					detailType : 0,
					transferStatus : 0,
				});

				break;
			}
		};
	}

}


app.controller('formSelfCtrl', ['$scope', '$http', FormSelfController]);
function FormSelfController($scope, $http){

	/*
	 * 数字框变动
	 */
	$scope.subGrid.numberChange = function(row, field) {
		var index = row.index;
		if (field == 'unitPrice' || field == 'quantity' || field == 'proxyPrice') {//单价 、数量、 代理价

			var unitPrice = 0;
			var quantity = 0;
			var proxyPrice = 0;
			if($scope.subGrid.sonlist[index]['unitPrice']) unitPrice = $scope.subGrid.sonlist[index]['unitPrice'];
			if($scope.subGrid.sonlist[index]['quantity']) quantity = $scope.subGrid.sonlist[index]['quantity'];
			if($scope.subGrid.sonlist[index]['proxyPrice']) proxyPrice = $scope.subGrid.sonlist[index]['proxyPrice'];

			var totalPrice = parseFloat(unitPrice) * parseFloat(quantity);
			var proxyTotalPrice = parseFloat(proxyPrice) * parseFloat(quantity);

			$scope.subGrid.sonlist[index]['totalPrice'] = totalPrice.toFixed(2);
			$scope.subGrid.sonlist[index]['proxyTotalPrice'] = proxyTotalPrice.toFixed(2);

		}
	}
}



app.controller('formSelfCtrl', ['$scope', '$http', FormSelfController]);
function FormSelfController($scope, $http){

	$scope.changeValueMap = function(field) {
		if(field == 'unitPrice' || field == 'quantity' || field == 'proxyPrice'){
			var unitPrice = parseFloat( $scope.valueMap["unitPrice"]); // 成本价
			var quantity = parseFloat( $scope.valueMap["quantity"]); // 数量
			var proxyPrice = parseFloat( $scope.valueMap["proxyPrice"]); // 代理价

			if((unitPrice || unitPrice==0) && (quantity || quantity==0)){
				var totalPrice = unitPrice*quantity;
				$scope.valueMap['totalPrice'] = totalPrice.toFixed(2);
			}
			if((quantity || quantity==0) && (proxyPrice || proxyPrice==0)){
				var proxyTotalPrice = proxyPrice*quantity;
				$scope.valueMap['proxyTotalPrice'] = proxyTotalPrice.toFixed(2);
			}
		}
	}

}


